package com.hevs.samplewebapp.server.json;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.DeserializationContext;
import org.codehaus.jackson.map.JsonDeserializer;

/**
 * Object used by Jackson to deserialize JSON date string to java.util.Date
 * 
 * @author Arnaud Durand
 * @date 29.03.2012
 */
public class DateDeserializer extends JsonDeserializer<Date> {

	/**
	 * Date formatter
	 */
	static SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy");

	/**
	 * Method to deserialize the Json
	 * @param jp
	 * Json to deserialize
	 * @param ctxt
	 * the context of deserialization
	 * @throws IOException, JsonProcessingException
	 * exception when the JSON isn't correct.
	 */
	@Override
	public Date deserialize(JsonParser jp, DeserializationContext ctxt)
			throws IOException, JsonProcessingException {
		try {
			return formatter.parse(jp.getText());
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}

}
